package com.unionftech.common.utils

import android.app.Application
import android.content.pm.ApplicationInfo
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.flattener.ClassicFlattener
import com.elvishew.xlog.formatter.message.xml.DefaultXmlFormatter
import com.elvishew.xlog.formatter.stacktrace.DefaultStackTraceFormatter
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.BackupStrategy2
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy2
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.elvishew.xlog.printer.file.writer.SimpleWriter


class LogUtils {
    companion object {
        private const val TAG = "LogUtils"
        private const val MAX_TIME = 1000L * 60L * 60L * 24L * 7L
        private lateinit var androidPrinter: AndroidPrinter
        private lateinit var filePrinter: FilePrinter
        private var isDebug: Boolean = true

        @JvmStatic
        fun init(context: Application, debug: Boolean) {
            isDebug = debug
            val logFilePath = context.getExternalFilesDir("")?.absolutePath + "/log"
            val config =
                LogConfiguration.Builder()
                    //调试的时候打印所有日志，上线之后只Info日志，并且是打印到文件中
                    .logLevel(if (isDebug) LogLevel.ALL else LogLevel.INFO)
                    .tag(TAG).xmlFormatter(DefaultXmlFormatter())
                    .stackTraceFormatter(DefaultStackTraceFormatter()).build()
            androidPrinter = AndroidPrinter(true)
            filePrinter =
                FilePrinter.Builder(logFilePath).fileNameGenerator(DateFileNameGenerator())
                    .backupStrategy(
                        FileSizeBackupStrategy2(
                            (500 * 1024 * 1024).toLong(), BackupStrategy2.NO_LIMIT
                        )
                    ).cleanStrategy(FileLastModifiedCleanStrategy(MAX_TIME))
                    .flattener(ClassicFlattener()).writer(SimpleWriter()).build()
            if (isDebug) {
                XLog.init(config, androidPrinter)
            } else {
                XLog.init(config, filePrinter)
            }
        }


        @JvmStatic
        fun v(tag: String, msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).tag(tag).v(msg)
            } else {
                XLog.printers(filePrinter).tag(tag).v(msg)
            }
        }

        @JvmStatic
        fun v(msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            XLog.v(msg)
        }

        @JvmStatic
        fun d(tag: String, msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).tag(tag).d(msg)
            } else {
                XLog.printers(filePrinter).tag(tag).d(msg)
            }
        }

        @JvmStatic
        fun d(msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            XLog.d(msg)
        }

        /**
         *  打印上线之后的重要信息
         */
        @JvmStatic
        fun i(tag: String, msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).tag(tag).i(msg)
            } else {
                XLog.printers(filePrinter).tag(tag).i(msg)
            }
        }

        /**
         * 打印上线之后的重要信息
         */
        @JvmStatic
        fun i(msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            XLog.i(msg)
        }

        @JvmStatic
        fun w(tag: String, msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).tag(tag).w(msg)
            } else {
                XLog.printers(filePrinter).tag(tag).w(msg)
            }
        }

        @JvmStatic
        fun w(msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            XLog.w(msg)
        }

        @JvmStatic
        fun e(tag: String, msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).tag(tag).e(msg)
            } else {
                XLog.printers(filePrinter).tag(tag).e(msg)
            }
        }

        @JvmStatic
        fun e(msg: String) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).e(msg)
            } else {
                XLog.printers(filePrinter).e(msg)
            }
        }

        @JvmStatic
        fun e(msg: String, error: Throwable?) {
            if (Companion::androidPrinter.isInitialized.not() || Companion::filePrinter.isInitialized.not()) {
                return
            }
            if (isDebug) {
                XLog.printers(androidPrinter, filePrinter).e(msg, error)
            } else {
                XLog.printers(filePrinter).e(msg, error)
            }
        }
    }
}
